import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'
import Home from '../components/Home.vue'
import User from '../components/user/user.vue'
import Item from '../components/items/Item.vue'
import ItemCat from '../components/items/ItemCat.vue'
import AddItemt from '../components/items/addItem.vue'
//使用路由机制
Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/elementUI', component: ElementUI},
  {path: '/home', component: Home,children:[
      {path: '/user', component: User},
      {path: '/Item', component: Item},
      {path: '/ItemCat', component: ItemCat},
      {path: '/Item/addItem', component: AddItemt}
  ]}

]

//路由导航守卫!!!!!!!

const router = new VueRouter({
  routes
})

/**
 * 定义路由导航卫士
 * 参数1：to 路由跳转的网址
 * 参数2：from 路由来自哪
 * 参数3：next 是一个函数表示放行或重定向，next()放行，next("/login")重定向*/
router.beforeEach((to,from,next)=>{
  if(to.path === "/login") return next()
  let token = window.sessionStorage.getItem("token")
  if(token){
    return next()
  }
  next("/login")

})

export default router
